package com.service.healthycode.task;


import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.service.config.utils.*;
import com.service.healthycode.dao.CloudCampusInfoDao;
import com.service.healthycode.dao.UserLoginRecordsDao;
import com.service.healthycode.dto.NceUploadData;
import com.service.healthycode.entity.CloudCampusInfo;
import com.service.healthycode.service.CloudCampusInfoService;
import com.service.healthycode.service.JsonDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.*;


@Slf4j
@Service
@Async
public class ScheduledService {
    @Resource
    RedisUtil redisUtil;
    @Resource
    private CloudCampusInfoDao cloudCampusInfoDao;
    @Resource
    private CloudCampusInfoService cloudCampusInfoService;
    @Resource
    CloudCampusApi cloudCampusApi;
    private final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
    private final ExecutorService product = new ThreadPoolExecutor(20,40,200L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),namedThreadFactory);
    @Resource
    private UserLoginRecordsDao userLoginRecordDao;
    @Resource
    private JsonDataService jsonDataService;
    /**
     * 用户定位数据
     * @throws Exception
     */
//    @Scheduled(cron = "0 0/1 * * * ?")
    //@Async("taskExecutorConfig")
    public void getLoginUserDispose12(NceUploadData lbsEntity) throws Exception {
        String s = PropertiesUtils.readJsonFile("data.json");
        Result result = JsonXMLUtils.json2obj(s,Result.class);
        NceUploadData nceUploadData = JsonXMLUtils.json2obj(result.getData().toString(), NceUploadData.class);
        String secret = nceUploadData.getSecret();
        CloudCampusInfo cloudCampusInfo = cloudCampusInfoService.queryBySecret(secret);
        //校验secret与预设是否相符
        if (cloudCampusInfo == null) {
        }
        jsonDataService.insertList(nceUploadData,cloudCampusInfo);
    }

    private  List<CloudCampusInfo> getCloudCampusInfo() {
        List<CloudCampusInfo> cloudCampusInfos = cloudCampusInfoDao.queryAll(new CloudCampusInfo());
        return cloudCampusInfos;
    }



    /**
     * 获取用户token
     * @param cloudCampusInfo
     */
    public HttpHeaders getToken(CloudCampusInfo cloudCampusInfo) {

        HttpHeaders requestHeaders = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        requestHeaders.setContentType(type);
        requestHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());

        String userName = cloudCampusInfo.getTenantName();
        String password = cloudCampusInfo.getTenantPwd();
        String token = "";

        try {
            if (redisUtil.exist(userName.concat("token"))) {
                token = redisUtil.getValue(userName.concat("token"));
            } else {
                //获取token
                token = cloudCampusApi.getToken(userName, password, requestHeaders, cloudCampusInfo.getDomain());
                redisUtil.set(userName.concat("token"), 1, token, 10);
            }
            //获取站点
            requestHeaders.add("X-AUTH-TOKEN", token);
            return requestHeaders;
        } catch (Exception e) {
            log.error("token Access failure!");
            return null;
        }

    }
}


